package com.dengyafei.controller;

import com.dengyafei.annotation.JwtToken;
import com.dengyafei.bean.EnterpriseEntity;
import com.dengyafei.constant.RecommendConstant;
import com.dengyafei.constant.ResultMessage;
import com.dengyafei.domin.vo.*;
import com.dengyafei.result.RestApiResult;
import com.dengyafei.service.EnterpriseService;
import com.dengyafei.utils.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.yarn.webapp.hamlet.Hamlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
 * @author 邓亚非
 */
@RestController
@RequestMapping("/enterprise")
@Slf4j
public class EnterpriseController {

    @Autowired
    private EnterpriseService enterpriseService;

    @PostMapping(value = "/enterpriseRes")
    public RestApiResult enterpriseRes(@RequestBody EnterpriseRegisterVO enterpriseRegisterVO) {
        log.info("【企业用户模块-注册接口-参数】enterpriseRegisterVO:[{}]", enterpriseRegisterVO);
        Integer integer = enterpriseService.enterpriseRegister(enterpriseRegisterVO);
        if (integer.equals(RecommendConstant.ALREADY_REGISTER)) {
            log.error("【企业用户模块-注册接口-失败】enterpriseRegisterVO,cause:[{}]", ResultMessage.Enterprise.ALREADY_REGISTER);
            return RestApiResult.error(ResultMessage.Enterprise.ALREADY_REGISTER);
        } else if (integer.equals(RecommendConstant.REGISTER_FAILURE)) {
            log.error("【企业用户模块-注册接口-失败】enterpriseRegisterVO,cause:[{}]", ResultMessage.Enterprise.REGISTER_FAILURE);
            return RestApiResult.error(ResultMessage.Enterprise.REGISTER_FAILURE);
        }
        log.info("【企业用户模块-注册接口-成功】enterpriseRegisterVO:[{}]", ResultMessage.Enterprise.REGISTER_SUCCESS);
        return RestApiResult.okMsg(ResultMessage.Enterprise.REGISTER_SUCCESS);
    }

    /**
     * 企业用户登录接口
     *
     * @param enterpriseLoginVO
     * @return
     */
    @PostMapping("/enterpriseLogin")
    public RestApiResult enterpriseLogin(@RequestBody EnterpriseLoginVO enterpriseLoginVO) {
        HashMap<String, String> map = new HashMap<>();
        log.info("【企业用户模块-登录接口-参数】enterpriseLoginVO:[{}]", enterpriseLoginVO);
        Integer integer = enterpriseService.enterpriseLogin(enterpriseLoginVO);
        if (integer.equals(RecommendConstant.NOT_REGISTER)) {
            log.error("【企业用户模块-登录接口-失败】enterpriseLoginVO,cause:[{}]", ResultMessage.Enterprise.NOT_REGISTER);
            return RestApiResult.error(ResultMessage.Enterprise.NOT_REGISTER);
        } else if (integer.equals(RecommendConstant.PASSWORD_ERROR)) {
            log.error("【企业用户模块-登录接口-失败】enterpriseLoginVO,cause:[{}]", ResultMessage.Enterprise.PASSWORD_ERROR);
            return RestApiResult.error(ResultMessage.Enterprise.PASSWORD_ERROR);
        }
        log.info("【企业用户模块-登录接口-成功】enterpriseLoginVO:[{}]", ResultMessage.Enterprise.LOGIN_SUCCESS);
        map.put("userId", enterpriseLoginVO.getUsername());
        String token = JwtUtil.generateToken(map);
        return RestApiResult.okMsg(token);
    }

    /**
     * 企业用户重置密码接口
     *
     * @param resetPasswordVO
     * @return
     */
    @PostMapping(value = "/enterpriseResetPassword")
    public RestApiResult enterpriseResetPassword(@RequestBody ResetPasswordVO resetPasswordVO) {
        log.info("【企业用户模块-重置密码接口-参数】resetPasswordVO:[{}]", resetPasswordVO);
        Integer integer = enterpriseService.enterpriseResetPassword(resetPasswordVO);
        if (integer.equals(RecommendConstant.RESET_FAILURE)) {
            log.error("【企业用户模块-重置密码接口-失败】resetPasswordVO,cause:[{}]", ResultMessage.Enterprise.RESET_FAILURE);
            return RestApiResult.error(ResultMessage.Enterprise.RESET_FAILURE);
        }
//        成功返回1
        log.info("【企业用户模块-重置密码接口-成功】resetPasswordVO:[{}]", ResultMessage.Enterprise.RESET_SUCCESS);
        return RestApiResult.okMsg(ResultMessage.Enterprise.RESET_SUCCESS);
    }

    /**
     * 企业用户查询个人信息
     *
     * @return
     */
    @JwtToken
    @PostMapping(value = "/queryEnterpriseUserinfo")
    public RestApiResult queryEnterpriseUserinfo() {
        log.info("【企业用户模块-查询个人信息接口-参数】:[{}]", "无参");
        EnterpriseEntity enterpriseEntity = enterpriseService.enterpriseQueryByName();
        log.error("【企业用户模块-查询个人信息接口-成功】", ResultMessage.Enterprise.ENTERPRISE_QUERY_INFO_SUCCESS);
        return RestApiResult.ok(enterpriseEntity);
    }

    /**
     * 企业用户修改个人信息
     *
     * @param enterpriseUpdateVO
     * @return
     */
    @JwtToken
    @PostMapping(value = "/updateEnterpriseInfo")
    public RestApiResult updateEnterpriseInfo(@RequestBody EnterpriseUpdateVO enterpriseUpdateVO) {
        log.info("【企业用户模块-修改个人信息接口-参数】enterpriseUpdateVO:[{}]", enterpriseUpdateVO);
        Integer integer = enterpriseService.enterpriseUpdateInfo(enterpriseUpdateVO);
        if (!integer.equals(RecommendConstant.OPERATION_SUCCESS)) {
            log.error("【企业用户模块-修改个人信息接口-失败】enterpriseUpdateVO,cause:[{}]", ResultMessage.Enterprise.ENTERPRISE_UPDATE_FAILURE);
            return RestApiResult.error(ResultMessage.Enterprise.ENTERPRISE_UPDATE_FAILURE);
        }
        log.info("【企业用户模块-修改个人信息接口-成功】enterpriseUpdateVO:[{}]", ResultMessage.Enterprise.ENTERPRISE_UPDATE_SUCCESS);
        return RestApiResult.okMsg(ResultMessage.Enterprise.ENTERPRISE_UPDATE_SUCCESS);
    }

    /**
     * 认证接口
     *
     * @param enterpriseUpdatePasswordVO
     * @return
     */
    @JwtToken
    @PostMapping(value = "/check")
    public RestApiResult check(@RequestBody EnterpriseUpdatePasswordVO enterpriseUpdatePasswordVO) {
        log.info("【企业用户模块-认证接口-参数】enterpriseUpdatePasswordVO:[{}]", enterpriseUpdatePasswordVO);
        Boolean aBoolean = enterpriseService.checkEnterprise(enterpriseUpdatePasswordVO);
        if (!aBoolean) {
            log.error("【企业用户模块-认证接口-失败】enterpriseUpdatePasswordVO,cause:[{}]", ResultMessage.Enterprise.CHECK_FAILURE);
            return RestApiResult.error(ResultMessage.Enterprise.CHECK_FAILURE);
        }
        log.info("【企业用户模块-认证接口-成功】enterpriseUpdatePasswordVO:[{}]", ResultMessage.Enterprise.CHECK_SUCCESS);
        return RestApiResult.okMsg(ResultMessage.Enterprise.CHECK_SUCCESS);
    }

    /**
     * 修改密码接口
     *
     * @param enterpriseUpdatePasswordVO
     * @return
     */
    @JwtToken
    @PostMapping(value = "/update")
    public RestApiResult update(@RequestBody EnterpriseUpdatePasswordVO enterpriseUpdatePasswordVO) {
        log.info("【企业用户模块-修改密码接口-参数】enterpriseUpdatePasswordVO:[{}]", enterpriseUpdatePasswordVO);
        Integer integer = enterpriseService.updatePassword(enterpriseUpdatePasswordVO);
        if (!integer.equals(RecommendConstant.OPERATION_SUCCESS)) {
            log.error("【企业用户模块-修改密码接口-失败】enterpriseUpdatePasswordVO,cause:[{}]", ResultMessage.Enterprise.UPDATE_FAILURE);
            return RestApiResult.error(ResultMessage.Enterprise.UPDATE_FAILURE);
        }
        log.info("【企业用户模块-修改密码接口-成功】enterpriseUpdatePasswordVO:[{}]", ResultMessage.Enterprise.UPDATE_SUCCESS);
        return RestApiResult.okMsg(ResultMessage.Enterprise.UPDATE_SUCCESS);
    }

    /**
     * 上传头像
     * @param multipartFile
     * @return
     */
    @CrossOrigin
    @PostMapping(value = "/upload")
    public String upload(@RequestParam("file")MultipartFile multipartFile) throws IOException {
        log.info("【企业用户模块-上传头像接口-参数】:[{}]",multipartFile);
        String string = Base64Utils.encodeToString(multipartFile.getBytes());
        String imgUrl=RecommendConstant.SRC_PREFIX+string;
        log.info("【企业用户模块-上传头像接口-成功】:[{}]",ResultMessage.Common.UPLOAD_SUCCESS);
        return imgUrl;
    }

    /**
     * 保存图片url到redis中
     * @param enterpriseHeadImageVO
     * @return
     */
    @JwtToken
    @PostMapping(value = "/save")
    public RestApiResult save(@RequestBody EnterpriseHeadImageVO enterpriseHeadImageVO){
        log.info("【企业用户模块-保存头像接口-参数】:[{}]",enterpriseHeadImageVO);
        Boolean aBoolean = enterpriseService.saveImg(enterpriseHeadImageVO);
        if (aBoolean.equals(false)){
            log.error("【企业用户模块-保存头像接口-失败】,cause:[{}]",ResultMessage.Common.SAVE_FAILURE);
            return RestApiResult.error(ResultMessage.Common.SAVE_FAILURE);
        }
        log.info("【企业用户模块-保存头像接口-成功】:[{}]",ResultMessage.Common.SAVE_SUCCESS);
        return RestApiResult.okMsg(ResultMessage.Common.SAVE_SUCCESS);
    }

    /**
     * 加在企业用户头像
     * @return
     */
    @JwtToken
    @PostMapping(value = "/loadHeadImage")
    public RestApiResult loadHeadImage(){
        log.info("【企业用户模块-加载头像接口-参数】:[{}]","无参");
        String s = enterpriseService.queryImg();
        log.info("【企业用户模块-加载头像接口-成功】:[{}]",ResultMessage.Common.LOAD_SUCCESS);
        return RestApiResult.okMsg(s);
    }

    /**
     * 查询所有企业用户信息
     * @return
     */
    @PostMapping(value = "/queryEnterprises")
    public RestApiResult queryEnterprises(){
        List<EnterpriseEntity> enterpriseEntities = enterpriseService.queryEnterprises();
        return RestApiResult.ok(enterpriseEntities);
    }
}